package com.baiku.sanhos.utils.model;

import com.baiku.sanhos.pojo.vo.UserInventoryListVO;
import com.baiku.sanhos.pojo.vo.UserInventoryVO;
import com.baiku.sanhos.response.ResultVO;
import com.baiku.sanhos.utils.model.core.CreatePrintData;
import com.baiku.sanhos.utils.model.entity.SanhosItemInventory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Sanhos数据模型 指定位置插入数据
 */
@Slf4j
@Component
public class SanhosDataModel {
    @Autowired
    CreatePrintData createPrintData;

    /**
     * 打印清单
     *
     * @param inventory UserInventoryVO 清单类
     * @return ResultVO
     */
    public ResultVO printInventory(UserInventoryVO inventory) {

        String name = inventory.getNickname();
        String date = inventory.getSchedulTime();
        String username = inventory.getUsername();
        String doctor = inventory.getDoctorName();
        String alipayQrCode = inventory.getAlipayQrCode();//支付宝生成的二维码内容
        //获取项目清单数据
        List<UserInventoryListVO> inventoryListVO = inventory.getUserInventoryListVO();
        List<SanhosItemInventory> inventoryList = new ArrayList<>();
        //集合数据输出到指定的集合中
        for (UserInventoryListVO listVO : inventoryListVO) {
            SanhosItemInventory itemInventory = new SanhosItemInventory();
            itemInventory.setItem(listVO.getDrugName());//药品名称
            itemInventory.setUnitPrice(listVO.getUnitPrice());//药品单价
            itemInventory.setCount(listVO.getNumber());//药品数量
            inventoryList.add(itemInventory);//添加对象元素
        }
        try {
            createPrintData.writeData(name, date, username,doctor, inventoryList,alipayQrCode);
            log.debug("打印完成");
        } catch (IOException e) {
            log.error("打印失败错误：{}", e.getMessage());
            return ResultVO.error("打印失败");
        }
        return ResultVO.ok("打印完成");
    }
}


